<?php

namespace Controller\Install;

use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;

class DefaultCSP implements ControllerProviderInterface {
    public function connect(Application $app) {
        $controllers = $app ['controllers_factory'];
        $controllers->match ( '/', function (Application $app) {
            
            $data = array (
                            'name' => 'Your name',
                            'email' => 'Your email' 
            );
            
            $form = $app ['form.factory']->createBuilder ( 'form', $data )->add ( 'name' )->add ( 'email' )->add ( 'gender', 'choice', array (
                            'choices' => array (
                                            1 => 'male',
                                            2 => 'female' 
                            ),
                            'expanded' => true 
            ) )->getForm ();
            
//             $form->handleRequest ( $request );
            
            // $user = new \Entity\User();
            // $user->setAcct('luckyweida@gmail.com');
            // $user->setPass(md5($user->getSalt() . '123456'));
            // $app ['em']->persist($user);
            // $app ['em']->flush();
            
            // $user = $app ['em']->find('\Entity\User', 1);
            
            // $purchase = new \Entity\Purchase();
            // $purchase->setStatus(1);
            // $purchase->setGpsAcct('xzk1950@gmail.com');
            // $purchase->setGpsPass('Pass1234');
            // $purchase->setOwner($user);
            // $app ['em']->persist($purchase);
            // $app ['em']->flush();
            
            // \Doctrine\Common\Util\Debug::dump($purchase);exit;
            
            if ($form->isValid ()) {
                $data = $form->getData ();
                
                // do something with the data
                
                // redirect somewhere
                return $app->redirect ( '...' );
            }
            
            // Render
            return $app ['twig']->render ( "home.twig", array (
                            'page' => array (
                                            'title' => '123' 
                            ) 
      ) );
    }, 'GET|POST' );
    
    return $controllers;
  }
}